//
//  SearchView.swift
//  新浪微博项目
//
//  Created by 王炯 on 16/7/23.
//  Copyright © 2016年 王炯. All rights reserved.
//

import UIKit

class SearchView: UIView
{
    //懒加载控件
    //(1)懒加载一个textfield
    private lazy var textField:UITextField = {
       
        let textF = UITextField()
        
        //设置textField的边框
        textF.layer.borderWidth = 2
        textF.layer.borderColor = THEMEColor.CGColor
        textF.placeholder = "搜索一下，你就知道"
        textF.backgroundColor = UIColor.whiteColor()
        
        //设置textField左边的view(imageView)
        let imgView = UIImageView(image:UIImage(named: "searchbar_textfield_search_icon"))
        
        //设置size和居中
        imgView.frame.size = CGSize(width: 35, height: 35)
        imgView.contentMode = .Center
        
        textF.leftView = imgView
        textF.leftViewMode = .Always
        
        
        //设置代理
        textF.delegate = self
        
        return textF
        
    }()
    
    //(2)懒加载一个取消按钮
    private lazy var cancelButton:UIButton = {
       
        let button = UIButton()
        
        button.addTarget(self, action: #selector(SearchView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("取消", forState: UIControlState.Normal)
        button.setTitleColor(THEMEColor, forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        
        return button
        
    }()
    
    
    //button的点击事件
    //点击button之后,textField延长
    @objc private func buttonClick()
    {
    
        
        //重新设置textField的约束
        self.textField.snp_updateConstraints(closure: { (make) in
            
            //尾部约束改变
            make.trailing.equalTo(self)
            
        })
        
        //动画效果
        UIView.animateWithDuration(0.25, animations: {
            
            //重新刷新界面
            self.layoutIfNeeded()
            
            
            }) { (_) in
                
                //一定要取消textField的第一响应者身份
                self.textField.resignFirstResponder()
                
        }
    }
    

    override init(frame: CGRect)
    {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 36))
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI()
    {
        //self.backgroundColor = UIColor.yellowColor()
        
        //添加控件
        addSubview(self.textField)
        addSubview(self.cancelButton)
        
        //设置约束
        cancelButton.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.top.bottom.trailing.equalTo(self)
        }
        textField.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
    }

}

//设置textField的代理方法
extension SearchView:UITextFieldDelegate
{
    //当开始编辑时的触发事件:点击之后textField伸缩
    //开始编辑，直到取消textField的第一响应者
    func textFieldDidBeginEditing(textField: UITextField)
    {
        
        //重新设置textField的约束
        self.textField.snp_updateConstraints { (make) in
            //尾部约束改变
            make.trailing.equalTo(self).offset(-50)
        }
        
        //动画效果
        UIView.animateWithDuration(0.25, animations: {
            
            //重新刷新界面
            self.layoutIfNeeded()
            
            
        }) { (_) in
            
        }
    }
}
